package com.example.cofeed.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.blankj.utilcode.util.ObjectUtils;
import com.example.cofeed.R;
import com.example.cofeed.activity.fragment.DemandListFragment;
import com.example.cofeed.adapter.BreedSelectAdapter;
import com.example.cofeed.adapter.PagerAdapter;
import com.example.cofeed.adapter.SubBreedSelectAdapter;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.IntentKeyConstant;
import com.example.cofeed.entity.Navigator;
import com.example.cofeed.entity.Standard;
import com.lzy.okgo.model.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BreedSelectActivity extends BaseActivity {

    @BindView(R.id.rv_first)
    RecyclerView mRvFirst;
    @BindView(R.id.rv_second)
    RecyclerView mRvSecond;
    private BreedSelectAdapter mBreedSelectAdapter;
    private SubBreedSelectAdapter mSubBreedSelectAdapter;
    private String firstStr = "";
    private int catid;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_breed_select;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        setMainTitle("品种");

        mRvFirst.setLayoutManager(new LinearLayoutManager(this));
        mBreedSelectAdapter = new BreedSelectAdapter();
        mRvFirst.setAdapter(mBreedSelectAdapter);

        mBreedSelectAdapter.setOnItemClickListener((adapter, view, position) -> {
            Navigator navigator = (Navigator) adapter.getData().get(position);
            mBreedSelectAdapter.setSelect(navigator);
            firstStr = navigator.title;
            catid = navigator.catid;
            getSubData(navigator);

        });

        mRvSecond.setLayoutManager(new LinearLayoutManager(this));
        mSubBreedSelectAdapter = new SubBreedSelectAdapter();
        mRvSecond.setAdapter(mSubBreedSelectAdapter);
        mSubBreedSelectAdapter.setOnItemClickListener((adapter, view, position) -> {
            Standard standard = (Standard) adapter.getData().get(position);
            Intent intent = new Intent();
            intent.putExtra(IntentKeyConstant.ID, catid);
            intent.putExtra(IntentKeyConstant.NAME, standard.name);
            intent.putExtra(IntentKeyConstant.FIRST_STR, firstStr);
            setResult(RESULT_OK, intent);
            finish();

        });


        ApiService.navigator_supply(this, new DialogCallback<LzyResponse<List<Navigator>>>(this) {
            @Override
            public void onSuccess(Response<LzyResponse<List<Navigator>>> response) {
                mBreedSelectAdapter.setNewData(response.body().data);
                mBreedSelectAdapter.setSelect(response.body().data.get(0));
                firstStr = response.body().data.get(0).title;
                catid = response.body().data.get(0).catid;
                getSubData(response.body().data.get(0));

            }
        });

    }

    private void getSubData(Navigator navigator){
        ApiService.navigator_supply_get_standard(this, navigator.catid, new DialogCallback<LzyResponse<List<Standard>>>(this) {
            @Override
            public void onSuccess(Response<LzyResponse<List<Standard>>> response) {
                mSubBreedSelectAdapter.setNewData(response.body().data);

                if (ObjectUtils.isEmpty(mSubBreedSelectAdapter.getData())){
                    View inflate = LayoutInflater.from(mContext).inflate(R.layout.include_no_data, null);
                    inflate.setVisibility(View.VISIBLE);
                    mSubBreedSelectAdapter.setEmptyView(inflate);
                }

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
